//
//  HomeTableViewCell.m
//  知乎日报
//
//  Created by 陈明光 on 2019/9/5.
//  Copyright © 2019 陈明光. All rights reserved.
//

#import "HomeTableViewCell.h"
@interface HomeTableViewCell()
//给cell加一层背景
@property(nonatomic,strong)UIView *bgView;
//cell里面有两个属性，一个图片，一段文字
@property(nonatomic,strong)UILabel *titlLabel;
@property(nonatomic,strong)UIImageView *iconImageView;
@end
@implementation HomeTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createbgView];
        
        //添加视图
        self.titlLabel=[UILabel new];
        [self.bgView addSubview:self.titlLabel];
        
        self.iconImageView=[UIImageView new];
        [self.bgView addSubview:self.iconImageView];
        
        //布局
        self.iconImageView.sd_layout.topSpaceToView(self.bgView, 10).leftSpaceToView(self.bgView, 10).widthIs(70).heightIs(60);
        self.titlLabel.sd_layout.topEqualToView(self.iconImageView).leftSpaceToView(self.iconImageView, 10).rightSpaceToView(self.bgView, 10).autoHeightRatio(0);
        
        [self.bgView setupAutoHeightWithBottomViewsArray:@[self.iconImageView,self.titlLabel] bottomMargin:10];
        //高度自适应
        [self setupAutoHeightWithBottomViewsArray:@[self.bgView] bottomMargin:5];
    }
    return self;
}
//cell背景
-(void)createbgView{
    self.bgView=[UIView new];
    self.bgView.backgroundColor=[UIColor whiteColor];
    
    //添加阴影
    self.bgView.layer.shadowOffset=CGSizeMake(1, 1);
    self.bgView.layer.shadowOpacity=0.3;
    self.bgView.layer.shadowColor=[UIColor blackColor].CGColor;
    
    [self.contentView addSubview:self.bgView];
    //自动布局
    self.bgView.sd_layout.topSpaceToView(self.contentView, 10).leftSpaceToView(self.contentView, 10).rightSpaceToView(self.contentView, 10);
    
}
-(void)setModel:(StroriesModel *)model{
    _model=model;
    self.titlLabel.text=model.title;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[model.images firstObject]]];
}
//点击cell的方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
    self.backgroundColor=[UIColor whiteColor];
    if (selected) {
        self.bgView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    }else{
        self.bgView.backgroundColor=[UIColor whiteColor];
    }
}

@end
